package active;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月21日 11:28
 * @Description TODO
 */
@WebServlet("/test05")
public class Test05Servlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        请求的配置 - 请求头
//              getHeader 获取当前请求的相关配置（获取请求头）
//                    + userAgent 获取用户客户端信息
        String userAgent = req.getHeader("User-Agent");
        System.out.println( userAgent );
//        加载被分割好的请求头信息
        Enumeration<String> headerNames = req.getHeaderNames();
        while ( headerNames.hasMoreElements() ){
            String name = headerNames.nextElement();
            System.out.println( name + ":" + req.getHeader(name) );
        }
//        获取客户端IP
        String ip = req.getRemoteAddr();
        System.out.println("客户端Ip:"+ip);
//        访问域名-服务器地址
        System.out.println("服务器地址:"+req.getRemoteHost());
        System.out.println("服务器端口:"+req.getRemotePort());
        System.out.println("访问的项目地址路径:"+req.getContextPath());
        System.out.println("访问的资源地址路径:"+req.getServletPath());

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("result");
    }
}
